package com.pinxiang.framework.weather.power.controller;

import com.alibaba.fastjson.JSON;
import com.pinxiang.framework.weather.power.model.MtHistoryData;
import com.pinxiang.framework.weather.power.service.IMtHistoryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/history")
public class HistoryDataController {
    @Autowired
    private IMtHistoryDataService mtHistoryDataService;

    @RequestMapping(value = "/dataGrid")
    @ResponseBody
    public String query(Integer pageSize, Integer pageNumber, String startTime, String endTime, String deviceCode, String equnum, HttpServletRequest request,
                        HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        int arg1 = (pageNumber - 1) * pageSize + 1;
        int arg2 = pageSize;
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("equnum", equnum);
        parameters.put("arg1", arg1);
        parameters.put("arg2", arg2);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<MtHistoryData> historyDatas = mtHistoryDataService.findAll(parameters);
        int total = mtHistoryDataService.selectCountByParams(parameters);
        resultMap.put("total", total);
        resultMap.put("rows", historyDatas);
        String json = JSON.toJSONString(resultMap);
        return json;
    }


    @RequestMapping(value = "/loadWd")
    @ResponseBody
    public String loadWd(String startTime, String endTime, String deviceCode, String flag, HttpServletRequest request,
                         HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("flag", flag);
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        if ((!"".equals(startTime) && startTime.equals(endTime)) || (!"".equals(startTime) && "".equals(endTime))) {
            List<MtHistoryData> selectList = mtHistoryDataService.selectForDisplay(parameters);
            String[] legend = new String[selectList.size()];
            String[] data = new String[selectList.size()];
            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                if (flag.equals("Hr_Temp_Avg")) {
                    data[i] = mt.getHrTempAvg();
                } else if (flag.equals("Hr_RH_Avg")) {
                    data[i] = mt.getHrRhAvg();
                } else if (flag.equals("Hr_Pvapor_Avg")) {
                    data[i] = mt.getHrPvaporAvg();
                } else if (flag.equals("Hr_Rain_Tot")) {
                    data[i] = mt.getHrRainTot();
                } else if (flag.equals("Hr_Press_Aag")) {
                    data[i] = mt.getHrPressAag();
                }

                legend[i] = mt.getDatetime().substring(11, 13);
            }
            list.add(legend);
            list.add(data);


        } else {
            List<MtHistoryData> selectList = mtHistoryDataService.selectForDisplayByTwoDays(parameters);
            String[] legend = new String[selectList.size()];
            String[] data = new String[selectList.size()];
            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                data[i] = mt.getHrTempAvg();
                legend[i] = mt.getDatetime();
            }
            list.add(legend);
            list.add(data);
        }
        result = JSON.toJSONString(list);

        return result;
    }

    @RequestMapping(value = "/loadFF")
    @ResponseBody
    public String loadFF(String startTime, String endTime, String deviceCode, String flag, HttpServletRequest request,
                         HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("flag", flag);
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        if ((!"".equals(startTime) && startTime.equals(endTime)) || (!"".equals(startTime) && "".equals(endTime))) {
            List<MtHistoryData> selectList = mtHistoryDataService.selectForDisplay(parameters);
            String[] legend = new String[selectList.size()];
            String[] data = new String[selectList.size()];
            String[] data2 = new String[selectList.size()];
            String[] data3 = new String[selectList.size()];
            String[] data4 = new String[selectList.size()];
            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                if (flag.equals("2minws")) {
                    data[i] = mt.getWs2minP();
                    data2[i] = mt.getWs2minPMax();
                    data3[i] = mt.getWs2minWyt();
                    data4[i] = mt.getWs2minWytMax();
                } else if (flag.equals("2minwd")) {
                    data[i] = mt.getWd2minP();
                    data2[i] = mt.getWd2minPMax();
                    data3[i] = mt.getWd2minWyt();
                    data4[i] = mt.getWd2minWytMax();
                } else if (flag.equals("10minws")) {
                    data[i] = mt.getWs10minP();
                    data2[i] = mt.getWs10minPMax();
                    data3[i] = mt.getWs10minWyt();
                    data4[i] = mt.getWs10minWytMax();
                } else if (flag.equals("10minwd")) {
                    data[i] = mt.getWd10minP();
                    data2[i] = mt.getWd10minPMax();
                    data3[i] = mt.getWd10minWyt();
                    data4[i] = mt.getWd10minWytMax();
                }

                legend[i] = mt.getDatetime().substring(11, 13);
            }
            list.add(legend);
            list.add(data);
            list.add(data2);
            list.add(data3);
            list.add(data4);


        } else {
            List<MtHistoryData> selectList = mtHistoryDataService.selectFFForDisplayByTwoDays(parameters);
            String[] legend = new String[selectList.size()];
            String[] data = new String[selectList.size()];
            String[] data2 = new String[selectList.size()];
            String[] data3 = new String[selectList.size()];
            String[] data4 = new String[selectList.size()];
            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                legend[i] = mt.getDatetime();
                if (flag.equals("2minws")) {
                    data[i] = mt.getWs2minP();
                    data2[i] = mt.getWs2minPMax();
                    data3[i] = mt.getWs2minWyt();
                    data4[i] = mt.getWs2minWytMax();
                } else if (flag.equals("2minwd")) {
                    data[i] = mt.getWd2minP();
                    data2[i] = mt.getWd2minPMax();
                    data3[i] = mt.getWd2minWyt();
                    data4[i] = mt.getWd2minWytMax();
                } else if (flag.equals("10minws")) {
                    data[i] = mt.getWd10minP();
                    data2[i] = mt.getWd10minPMax();
                    data3[i] = mt.getWd10minWyt();
                    data4[i] = mt.getWd10minWytMax();
                } else if (flag.equals("10minwd")) {
                    data[i] = mt.getWd10minP();
                    data2[i] = mt.getWd10minPMax();
                    data3[i] = mt.getWd10minWyt();
                    data4[i] = mt.getWd10minWytMax();
                }
            }
            list.add(legend);
            list.add(data);
            list.add(data2);
            list.add(data3);
            list.add(data4);
        }
        String[] name;
        if (flag.equals("2minws")) {
            name = new String[]{"2min超声风平均风速", "2min超声风最大风速", "2min机械风平均风速", "2min机械风最大均风速"};
            list.add(name);
        } else if (flag.equals("2minwd")) {
            name = new String[]{"2min超声风平均风向", "2min超声风最大风向", "2min机械风平均风向", "2min机械风最大均风向"};
            list.add(name);
        } else if (flag.equals("10minws")) {
            name = new String[]{"10min超声风平均风速", "10min超声风最大风速", "10min机械风平均风速", "10min机械风最大均风速"};
            list.add(name);
        } else if (flag.equals("10minwd")) {
            name = new String[]{"10min超声风平均风向", "10min超声风最大风向", "10min机械风平均风向", "10min机械风最大均风向"};
            list.add(name);
        }
        result = JSON.toJSONString(list);

        return result;
    }

    @RequestMapping(value = "/loadWSForIndex")
    @ResponseBody
    public String loadWSForIndex(String startTime, String endTime, String deviceCode, String flag, HttpServletRequest request,
                         HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("flag", flag);
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        if ((!"".equals(startTime) && startTime.equals(endTime)) || (!"".equals(startTime) && "".equals(endTime))) {
            List<MtHistoryData> selectList = mtHistoryDataService.selectForDisplay(parameters);
            String[] legend = new String[selectList.size()];
            String[] data = new String[selectList.size()];
            String[] data2 = new String[selectList.size()];

            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                if (flag.equals("2minws")) {
                    data[i] = mt.getWs2minP();
                    data2[i] = mt.getWs2minWyt();

                } else if (flag.equals("2minwd")) {
                    data[i] = mt.getWd2minP();
                    data2[i] = mt.getWd2minWyt();

                } else if (flag.equals("10minws")) {
                    data[i] = mt.getWs10minP();

                    data2[i] = mt.getWs10minWyt();

                } else if (flag.equals("10minwd")) {
                    data[i] = mt.getWd10minP();

                    data2[i] = mt.getWd10minWyt();

                }

                legend[i] = mt.getDatetime().substring(11, 13);
            }
            list.add(legend);
            list.add(data);
            list.add(data2);



        } else {
            List<MtHistoryData> selectList = mtHistoryDataService.selectFFForDisplayByTwoDays(parameters);
            String[] legend = new String[selectList.size()];
            String[] data = new String[selectList.size()];
            String[] data2 = new String[selectList.size()];

            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                legend[i] = mt.getDatetime();
                if (flag.equals("2minws")) {
                    data[i] = mt.getWs2minP();
                    data2[i] = mt.getWs2minWyt();

                } else if (flag.equals("2minwd")) {
                    data[i] = mt.getWd2minP();
                    data2[i] = mt.getWd2minWyt();

                } else if (flag.equals("10minws")) {
                    data[i] = mt.getWs10minP();

                    data2[i] = mt.getWs10minWyt();

                } else if (flag.equals("10minwd")) {
                    data[i] = mt.getWd10minP();

                    data2[i] = mt.getWd10minWyt();

                }
            }
            list.add(legend);
            list.add(data);
            list.add(data2);

        }
        String[] name;
        if (flag.equals("2minws")) {
            name = new String[]{"2min超声风速", "2min机械风速",};
            list.add(name);
        } else if (flag.equals("2minwd")) {
            name = new String[]{"2min超声风向",  "2min机械风向",};
            list.add(name);
        } else if (flag.equals("10minws")) {
            name = new String[]{"10min超声风速",  "10min机械风速"};
            list.add(name);
        } else if (flag.equals("10minwd")) {
            name = new String[]{"10min超声风向",  "10min机械风向"};
            list.add(name);
        }
        result = JSON.toJSONString(list);

        return result;
    }

    @RequestMapping(value = "/loadWDForIndex")
    @ResponseBody
    public String loadWDForIndex(String startTime, String endTime, String deviceCode, String flag, HttpServletRequest request,
                                  HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("flag", flag);
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] direction = new String[]{"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"};
        if ((!"".equals(startTime) && startTime.equals(endTime)) || (!"".equals(startTime) && "".equals(endTime))) {
            List<MtHistoryData> selectList = mtHistoryDataService.selectForDisplay(parameters);
            int[] data = new int[16];


            for (int i = 0; i < selectList.size(); i++) {
                MtHistoryData mt = selectList.get(i);
                mt.setWd2minP(degreesTodirection(mt.getWd2minP()));



                if (flag.equals("2minws")) {


                }

            }
            Map<String, List<MtHistoryData>> groupBy = selectList.parallelStream().collect(Collectors.groupingBy(MtHistoryData::getWd2minP));


          for (int i=0;i<16;i++){
              String mm = direction[i];
              boolean p = false;
              for (Map.Entry<String, List<MtHistoryData>> entry : groupBy.entrySet()) {
                  if(mm.equals(entry.getKey())){
                      data[i]=entry.getValue().size();
                      p=true;
                  }
              }
              if(!p){
                  data[i]=0;
              }
          }

          list.add(data);
          list.add(selectList.size());
          list.add(direction);


        }
        result = JSON.toJSONString(list);

        return result;
    }

    private String degreesTodirection(String wd){
        String[] direction = new String[]{"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"};
        int i = new Double((Float.parseFloat(wd) + 11.25)/22.5).intValue();
        String dir = direction[i%16];
        return dir;
    }

}
